<?php

declare (strict_types=1);

namespace App\Model;

use App\Constants\RedisKey;
use Hyperf\DbConnection\Model\Model;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;

/**
 * @property int $id
 * @property string $name
 * @property int $type
 * @property int $platform
 * @property string $hash
 * @property int $status
 * @property string $app_id
 * @property string $app_secret
 * @property string $app_name
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 */
class QmGameList extends Model {
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'qm_game_list';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'type' => 'integer', 'platform' => 'integer', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public function getGameInfo($param): array {
        $redis = ApplicationContext::getContainer()->get(Redis::class);
        $gameInfo = $redis->get(RedisKey::GM_INFO . ':' . $param);
        if (empty($gameInfo)) {
            if (is_numeric($param)) {
                $gameInfo = self::query()->find($param);
            } else {
                $gameInfo = self::query()->where('hash', $param)->first();
            }
            if ($gameInfo) {
                $gameInfo = $gameInfo->toArray();
                $redis->set(RedisKey::GM_INFO . ':' . $param, json_encode($gameInfo), 3600);
            } else {
                return [];
            }
        } else {
            $gameInfo = json_decode($gameInfo, true);
        }

        return $gameInfo;
    }

}